/**
 * Created by Administrator on 2017/7/20.
 */
var total=2000,t1,t2,t3,d1,d2,d3,d4,o1,o2,dgtotal,obj1,obj2,obj3,obj4;
var op1={suffix:'册'}, op2={suffix:'人次'}, op3={suffix:'人次 '},jy=0,dg=0,csz=0,csz1=0;
var v11,v12,v13,v21,v22,v23,v24;
var ubw=0,uph=0;
$(function () {
    JYfun();
    DGfun();
    everyhourrun();
    run();
})
function  run() {
    if(csz==0){
        csz=1;
        window.setTimeout("run()",6000);
    }else{
        window.setTimeout("run()",12000);
    }
    changejys();
}
function  everyhourrun() {
    /*$(".lbar-warp .default").height(0);*/
    /*if(csz1==0){
     csz1=1;
     window.setTimeout("everyhourrun()",6000);
     }else{

     }*/
    window.setTimeout("everyhourrun()",10000);
    changedgs();
}
function changedgs(){
    if(dg==1) {
        obj1 = $(".dg1 .timer");
        obj2 = $(".dg2 .timer");
        obj3 = $(".dg3 .timer");
        $(".lbar-warp .timer").hide();
        $(".lbar-warp .timer").height(0);
        $(".lbar-warp .timer").show();
        ubw=$(".bar-warp").width();
        uph=$(".lbar-warp").height()
        v21=$("#jrdg").val();
        v22=$("#dqzg").val();
        v23=$("#bzdg").val();
        v24=$("#ljdg").val();
        var w1 = (v21 /total)*uph;
        obj1.height(parseInt(w1));
        obj1.addClass('active');
        var w2 = (v22 / total)*uph;
        obj2.height(parseInt(w2));
        obj2.addClass('active');
        var w3 = v23 / (total * 7)*uph;
        obj3.height(parseInt(w3));
        obj3.addClass('active');
        /*  d1.reset();
         d2.reset();
         d3.reset();
         d1.update(v21);
         d2.update(v22);
         d3.update(v23);*/
        /*d1 = new CountUp($(".dg1 .timer"), v21, 0, 0, 2, op2);
         d2 = new CountUp($(".dg2 .timer"), v22, 0, 0, 2, op2);
         d3 = new CountUp($(".dg3 .timer"), v23, 0, 0, 2, op2);
         d1.start();
         d2.start();
         d3.start();
         window.setTimeout(function(){
         obj1 = $(".dg1 .timer");
         obj2 = $(".dg2 .timer");
         obj3 = $(".dg3 .timer");
         v21 = Random.range1(v21, total, Math.ceil(Math.random() * 20))
         if (v21 / total >= 1) {
         v21 = o2.todayTomuseum;
         }
         ubw=$(".bar-warp").width();
         uph=$(".lbar-warp").height()
         v21=$("#jrdg").val();
         v22=$("#dqzg").val();
         v23=$("#bzdg").val();
         v24=$("#ljdg").val();
         var w1 = (v21 /total)*uph;
         obj1.height(parseInt(w1));
         d1.reset();
         d1.update(v21);
         d1 = new CountUp($(".dg1 .timer"), 0, v21, 0, 4, op2);
         d1.start();
         obj1.addClass('active');
         var w2 = (v22 / total)*uph;
         obj2.height(parseInt(w2));
         obj2.addClass('active');
         d2 = new CountUp($(".dg2 .timer"), 0, v22, 0, 4, op2);
         d2.start();
         d2.reset();
         d2.update(v22);
         var w3 = v23 / (total * 7)*uph;
         obj3.height(parseInt(w3));
         d3 = new CountUp($(".dg3 .timer"), 0, v23, 0, 4, op2);
         d3.start();
         d3.reset();
         d3.update(v23);
         },4000);*/
        var now = new Date();
        var date = new Date('2016/01/01 08:00:00');
        var diff = now.diff(date);
        var w4 = parseInt(v24 / (total*parseInt(diff))*ubw);
        obj4 = $(".b-content-ub .timer");
        obj4.width(w4);
        obj4.addClass('active');
        d4.reset();
        d4.update(v24);
    }
}
function changejys(){
    if(jy==1){
        /*v11=formatnum(v11,total),v12=formatnum(v12,total),v13=formatnum(v13,total*parseInt(diff));
         if(v11>=total){v11=o1.todayBorrowing;}
         if(v12>=total*7){v12=o1.weekBorrowing;}
         if(v13>=total*parseInt(diff)){v13=o1.addupBorrowing;}*/
        t1.reset();
        t1.update($("#jrjy").val());
        setTimeout(function(){ t2.reset();t2.update($("#bzjy").val());}, 4000);
        setTimeout(function(){ t3.reset();t3.update($("#ljjy").val());}, 8000);
    }
}
function formatnum(v,num){
    return Random.range1(v, num,Math.ceil(Math.random()*10));
}
function  ifnull(v) {
    if(v==null||v==""){
        v=0;
    }
    return v;
}
Date.prototype.diff = function(date){
    return (this.getTime() - date.getTime())/(24 * 60 * 60 * 1000);
}
function  JYfun() {
    $.ajax({
        url : 'view/getjynum.do',
        dataType : "json", // 返回格式为json
        ifModified:true,
        type : "POST", // 请求方式
        success : function(req) {
            if(req.length>0){
                o1=req[0];
                v11=ifnull(o1.todayBorrowing);v12=ifnull(o1.weekBorrowing);v13=ifnull(o1.addupBorrowing);
                $("#jrjy").val(v11);
                $("#bzjy").val(v12);
                $("#ljjy").val(v13);
                if(jy==0){
                    t1 = new CountUp($(".tjread1 .timer"), 0, v11,0,4,op1);
                    t2 = new CountUp($(".tjread2 .timer"), 0, v12,0,4,op1);
                    t3 = new CountUp($(".tjread3 .timer"), 0, v13,0,4,op1);
                    t1.start();t2.start();t3.start();
                    jy=1;
                }else{
                    t1.update(v11);
                    t2.update(v12);
                    t3.update(v13);
                }
            }
        }
    });
}

function DGfun(){
    $.ajax({
        url : 'view/getdgyji.do',
        dataType : "json", // 返回格式为json
        ifModified:true,
        type : "POST", // 请求方式
        success : function(req) {
            o2 = req.citylist[0];
            dgtotal=req.citylist[1];
            var now = new Date();
            var date = new Date('2016/01/01 08:00:00');
            var diff = now.diff(date);
            total=dgtotal.jrdgnum;
            v21=ifnull(o2.todayTomuseum);v22=ifnull(o2.currentlyInmuseum);v23=ifnull(o2.weekTomuseum);v24=ifnull(o2.accumulatedMuseum);
            $("#jrdg").val(v21);
            $("#dqzg").val(v22);
            $("#bzdg").val(v23);
            $("#ljdg").val(v24);
            $("#tjrdg").val(total);
            obj1 = $(".dg1 .timer");
            obj2 = $(".dg2 .timer");
            obj3 = $(".dg3 .timer");
            $(".lbar-warp .timer").hide();
            $(".lbar-warp .timer").height(0);
            $(".lbar-warp .timer").show();
            obj1.height((v21/total)*100+"%");
            obj2.height((v22/total)*100+"%");
            obj3.height((v23/(total*7))*100+"%");
            $(".b-content-ub .timer").width((v24/(total*parseInt(diff)))*100+"%");
            if(dg==0){
                $(".box21.timer").addClass('active');
                d1 = new CountUp($(".dg1 .timer"), 0, v21, 0, 3, op2);
                d2 = new CountUp($(".dg2 .timer"), 0, v22, 0, 3, op2);
                d3 = new CountUp($(".dg3 .timer"), 0, v23, 0, 3, op2);
                d4 = new CountUp($(".b-content-ub .timer"), 0, v24, 0, 3, op3);
                d1.start();
                d2.start();
                d3.start();
                d4.start();
                dg=1;
            }else{
                d1.update(v21);
                d2.update(v22);
                d3.update(v23);
                d4.update(v24);
            }
        }
    });
}


